﻿using System;
using System.Collections.Generic;
using System.Text;
using Imps.Client.Core;
using Imps.Client.Pc;
using com.hetaoos.FetionSDK.Event;
using Imps.Client;

namespace com.hetaoos.FetionSDK.Message
{
    public class SendMultiIMControl
    {
        private FetionSDK _frameworkWin = null;

        private AsyncBizOperation op;
        private List<Contact> _listContacts;
        private ContactBase _contactBase;

        public SendMultiIMControl(FetionSDK framework, ContactBase contactBase)
        {
            _frameworkWin = framework;
            _contactBase = contactBase;
            if (this.op == null)
            {
                this.op = new AsyncBizOperation();
                this.op.Successed += new EventHandler(this.op_Successed);
                this.op.ImpsError += new EventHandler<ImpsErrorEventArgs>(this.op_ImpsError);
            }
        }

        /// <summary>
        /// 给列表中的手机号或者飞信号发送消息
        /// </summary>
        /// <param name="list"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public int SendIM(string id, string msg)
        {
            List<string> list = new List<string>();
            list.Add(id);
            return SendIM(list, msg, false);
        }

        public int SendIM(List<string> list, string msg)
        {
            return SendIM(list, msg, false);
        }
        /// <summary>
        /// 给列表中的手机号或者飞信号发送消息
        /// </summary>
        /// <param name="list"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public int SendIM(List<string> list, string msg, bool OneConversation)
        {
            if (list == null || list.Count == 0)
            {
                return -1;
            }
            List<Contact> selectedContacts = new List<Contact>();
            _listContacts = _contactBase.getAllContactList();
            int index = 0;
            if (_listContacts == null || _listContacts.Count == 0)
            {
                return -1;
            }
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].Length == 11)
                {
                    index = findContactByMobileNo(list[i]);
                }
                else
                {
                    index = findContactBySID(list[i]);
                }

                if (index >= 0)
                {
                    selectedContacts.Add(_listContacts[index]);
                }
                else
                {
                    //ErrorLog.WriteLog("没有找到该用户：" + list[i]);
                    _frameworkWin.doSDK_Error(this, ErrorType.SendSMS, "没有找到该用户：" + list[i]);
                }
            }

            return sendIM_Base(ref selectedContacts, ref msg);
        }

        private int sendIM_Base(ref List<Contact> selectedContacts, ref string msg)
        {
            return sendIM_Base(ref selectedContacts, ref msg, false);
        }
        /// <summary>
        /// 发送消息基函数
        /// </summary>
        /// <param name="selectedContacts"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        private int sendIM_Base(ref List<Contact> selectedContacts, ref string msg, bool OneConversation)
        {
            if (selectedContacts == null || selectedContacts.Count == 0)
            {
                return 0;
            }
            if (OneConversation)
            {
                _frameworkWin.ConversationControl.SendIM(selectedContacts, msg);
            }
            else
            {
                for (int i = 0; i < selectedContacts.Count; i++)
                {
                    _frameworkWin.ConversationControl.SendIM(selectedContacts[i], msg);
                }
            }
            //if (selectedContacts.Count == 0)
            //{
            //    return 0;
            //}
            //else
            //{
            //    if (this.op == null)
            //    {
            //        this.op = new AsyncBizOperation();
            //        this.op.Successed += new EventHandler(this.op_Successed);
            //        this.op.ImpsError += new EventHandler<ImpsErrorEventArgs>(this.op_ImpsError);
            //    }
            //    foreach (Contact contact in selectedContacts)
            //    {
            //        if ((contact.Uri.Raw ==this._frameworkWin.CurrentUser.Uri.Raw) || contact.EnableSMS)
            //        {
            //            SendSMSItem item = new SendSMSItem(contact, msg);
            //            this._stackSMSItems.Enqueue(item);
            //        }
            //        //contact.
            //    }
            //    if ((this._stackSMSItems.Count > 0) && !this.sending)
            //    {
            //        //this.StartSend();
            //    }
            //}

            return 1;
        }

        /// <summary>
        /// 发送消息成功
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void op_Successed(object sender, EventArgs e)
        {
        }

        /// <summary>
        /// 发送消息失败
        /// </summary>
        private void op_ImpsError(object sender, ImpsErrorEventArgs e)
        {


        }

        /// <summary>
        /// 通过手机号找用户
        /// </summary>
        /// <param name="mobile"></param>
        /// <returns></returns>
        public int findContactByMobileNo(string mobile)
        {
            if (mobile.Length != 11)
            {
                return -1;
            }
            for (int i = 0; i < _listContacts.Count; i++)
            {
                if (_listContacts[i].PersonalInfo.MobileNo == mobile)
                {
                    return i;
                }
            }
            return -1;
        }

        /// <summary>
        /// 通过飞信号找用户
        /// </summary>
        /// <param name="sid"></param>
        /// <returns></returns>
        private int findContactBySID(string sid)
        {
            for (int i = 0; i < _listContacts.Count; i++)
            {
                if (_listContacts[i].Uri.Id == sid)
                {
                    return i;
                }
            }
            return -1;
        }
        /// <summary>
        /// 通过飞信号找手机号
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string getMobileNo(string id)
        {
            if (_listContacts == null)
            {
                _listContacts = _contactBase.getAllContactList();
            }
            for (int i = 0; i < _listContacts.Count; i++)
            {
                if (_listContacts[i].Uri.Id == id)
                {
                    return _listContacts[i].PersonalInfo.MobileNo;
                }
            }
            //ErrorLog.WriteLog("无法找到该飞信号用户的手机号：" + id);
            return id;
        }
    }
}
